package com.atguigu.srb.core.controller.admin;


import com.atguigu.common.result.R;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author lupengning
 * @since 2021-05-27
 */
@Api(tags = "积分等级管理")
//@CrossOrigin
@RestController
@RequestMapping("/admin/core/integralGrade")
public class AdminIntegralGradeController {

  @Resource
  private IntegralGradeService integralGradeService;

  /**
   * 获取积分等级列表
   * @return
   */
  @ApiOperation(value = "积分等级列表")
  @GetMapping("/list")
  public R listAll() {
    return R.ok().data("list", integralGradeService.list()).message("获取列表成功");
  }

  /**
   * 根据Id删除数据
   * @param id
   * @return
   */
  @ApiOperation(value = "根据Id删除数据",notes = "逻辑删除数据")
  @DeleteMapping("/remove/{id}")
  public R removeById(@PathVariable Long id) {
    boolean result = integralGradeService.removeById(id);
    if (result) {
      return R.ok().message("删除成功");
    }
    return R.error().message("删除失败");
  }

  /**
   * 新增积分等级
   * @param integralGrade
   * @return
   */
  @ApiOperation(value = "新增积分等级")
  @PostMapping("/save")
  public R save(@ApiParam(value = "新增积分等级对象",required = true)
                @RequestBody IntegralGrade integralGrade) {
    boolean result = integralGradeService.save(integralGrade);
    if (!result){
      return R.error().message("保存失败");
    }
    return R.ok().message("保存成功");
  }

  /**
   * 根据Id获取积分等级
   * @param id
   * @return
   */
  @ApiOperation(value = "根据Id获取积分等级")
  @GetMapping("/get/{id}")
  public R getById(@PathVariable Long id) {
    IntegralGrade integralGrade = integralGradeService.getById(id);
    if (integralGrade == null) {
      return R.error().message("获取失败");
    }
    return R.ok().message("获取成功").data("record",integralGrade);
  }

  /**
   * 更新Id获取积分等级
   * @param integralGrade
   * @return
   */
  @ApiOperation(value = "更新Id获取积分等级")
  @PutMapping("/update")
  public R getById(@RequestBody IntegralGrade integralGrade) {
    boolean result = integralGradeService.updateById(integralGrade);
    if (!result) {
      return R.error().message("更新失败");
    }
    return R.ok().message("更新成功");
  }

}

